package com.getpebble.android.core.analytics;

import android.content.Context;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.common.core.trace.AnalyticsLogger;
import com.getpebble.android.common.core.trace.AnalyticsLogger.Event;
import com.getpebble.android.common.core.trace.AnalyticsLogger.Forwarder;
import com.getpebble.android.common.core.trace.AnalyticsLogger.InitializationException;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ProcessUtil;
import com.getpebble.android.common.core.util.ProcessUtil.PebbleProcess;
import com.getpebble.android.framework.PblFrameworkInterface;

public class AnalyticsSetup {
    public static final String TAG = AnalyticsSetup.class.getSimpleName();

    public static void initializeMobileAnalytics(Context appContext) {
        try {
            AnalyticsLogger.initialize(appContext, AnalyticsLogger.createConfig(appContext), createForwarder(appContext));
        } catch (InitializationException e) {
            Trace.error(TAG, "Unable to initialize analytics!");
        } catch (IllegalStateException e2) {
            Trace.error(TAG, "Analytics was initialized ... before PebbleApplication.onCreate?");
        } catch (RuntimeException e3) {
            Trace.error(TAG, "Unexpected runtime problem when initializing analytics.");
        }
    }

    private static Forwarder createForwarder(Context appContext) {
        if (PebbleProcess.FRAMEWORK.equals(ProcessUtil.getProcess(appContext))) {
            return null;
        }
        return new Forwarder() {
            public void forwardAnalyticsMessage(Event event) {
                PblFrameworkInterface pblFrameworkInterface = PebbleApplication.getFrameworkInterface();
                if (pblFrameworkInterface == null) {
                    Trace.debug(AnalyticsSetup.TAG, "pblFrameworkInterface is null");
                } else {
                    pblFrameworkInterface.sendAnalyticsEvent(event);
                }
            }

            public void uploadAnalytics() {
                PblFrameworkInterface pblFrameworkInterface = PebbleApplication.getFrameworkInterface();
                if (pblFrameworkInterface == null) {
                    Trace.debug(AnalyticsSetup.TAG, "pblFrameworkInterface is null");
                } else {
                    pblFrameworkInterface.sendAnalyticsUploadRequest();
                }
            }
        };
    }

    public static void requestReinitializeMobileAnalytics() {
        PblFrameworkInterface pblFrameworkInterface = PebbleApplication.getFrameworkInterface();
        if (pblFrameworkInterface != null) {
            pblFrameworkInterface.sendAnalyticsReinitRequest();
        }
    }

    public static void terminateMobileAnalytics() {
        PblFrameworkInterface pblFrameworkInterface = PebbleApplication.getFrameworkInterface();
        if (pblFrameworkInterface != null) {
            pblFrameworkInterface.sendAnalyticsUploadRequest();
        }
        AnalyticsLogger.terminate();
    }
}
